<template>
  <div>
    <div>
      <van-nav-bar fixed>
        <template #right>
          <van-icon name="search" size="0.48rem" color="#ffff" @click="search" />
        </template>
        <template #left>
          <img class="logo" src="@/assets/logo.png" alt />
        </template>
      </van-nav-bar>
    </div>

    <div class="main">
      <van-tabs v-model="activeid" sticky offset-top="1.226667rem" animated>
        <van-tab :title="item.name" :name="item.id" v-for="item in UserChannelslist" :key="item.id">
          <ArticleList :activeId="activeid"></ArticleList>
        </van-tab>
      </van-tabs>
      <van-icon name="plus" class="moreChannels" size="0.37333333rem"  @click="showPopupclick" />
    </div>

    <!-- 弹出 -->
    <van-popup v-model="show" class="channels_poput">
      <ChannelEdit
      @ChannelEV="channelpush"
      :added="UserChannelslist"
      :unChannel="unCheckChannelslist"
      @close="panel"
      @deleteObj="delChannels"
      v-model="activeid"
      >
      </ChannelEdit>
    </van-popup>
  </div>
</template>

<script>
import { getUserChannelsAPI, getAllChannelsAPI } from '@/api/index.js'
import ArticleList from './components/ArticleList.vue'
import ChannelEdit from './ChannelEdit.vue'
export default {
  data () {
    return {
      activeid: 0,
      UserChannelslist: [],
      ArrChannelslist: [],
      show: false
    }
  },
  created () {
    this.navarr()
    this.arruser()
  },
  methods: {
    // 获取用户频道列表
    async navarr () {
      const { data: res } = await getUserChannelsAPI()
      this.UserChannelslist = res.data.channels
      console.log(res)
      // 文章列表
    },
    // 获取所有频道列表
    async arruser () {
      const { data: res } = await getAllChannelsAPI()
      this.ArrChannelslist = res.data.channels
      console.log(res)
      console.log(this.unCheckChannelslist)
    },
    showPopupclick () {
      this.show = true
    },
    // 添加频道
    async channelpush (obj) {
      this.UserChannelslist.push(obj)
    // 问题
      // const res = await updateUserChannelAPI({
      //   channels: this.UserChannelslist
      // })
      // console.log(res)
    },
    // 关闭弹出层
    panel (close) {
      this.show = close
    },
    // 删除频道1
    delChannels (itemChannele) {
      const index = this.UserChannelslist.findIndex(obj => obj.id === itemChannele.id)
      this.UserChannelslist.splice(index, 1)
    },
    // 点击搜索小图标 进行路由跳转
    search () {
      this.$router.push({ path: '/search' })
    }

  },
  computed: {
    // 目标“把所有频道数组挨个对象取出来 去用户已选频道数组里查找 如果找不到 则让filter方法收集到一个新数组里
    unCheckChannelslist () {
      // const newarr = this.ArrChannelslist.filter(bigObj => {
      //   const index = this.UserChannelslist.findIndex(smallObj => {
      //     return smallObj.id === bigObj.id
      //   })
      //   if (index > -1) {
      //     return false
      //   } else {
      //     return true
      //   }
      // })
      // return newarr
      return this.ArrChannelslist.filter(bigObj => (this.UserChannelslist.findIndex(smallObj =>
        smallObj.id === bigObj.id) === -1
      ))
    }
  },
  components: { ArticleList, ChannelEdit }
}
</script>

<style lang="less" scoped>
.main {
  position: relative;
  padding-top: 46px;
}

.van-nav-bar {
  background-color: #0077ff;
}
/deep/.van-tabs__line {
  background-color: #0077ff;
}
.logo {
  width: 35px;
  height: 30px;
}
/deep/ .van-tabs__wrap {
  padding-right: 30px;
  background-color: #fff;
}
.moreChannels {
  position: fixed;
  right:0px;
  top:  50px;
  width: 25px;
  height: 40px;
  text-align: center;
  line-height: 30px;
  z-index: 99;
}
.channels_poput {
  width: 100vw;
  height: 100vh;
}
</style>
